<?php

namespace App\Http\Resources;

use App\Http\Responses\Resource;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class Bar extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {

        $user = Auth::guard('api')->user();

        return [

            'id' => $this->id,
            'bar_num' => $this->bar_num,
            'title' => $this->title,
            'bar_cover' => $this->bar_cover,
            'bar_excerpt' => $this->bar_excerpt,
            'follow_num' => $this->fans()->where('status',0)->count(),
            'posts_count' => $this->posts()->count(),
            'is_follow' => $this->when($user,$this->isFollow($user))

        ];

    }


    protected function isFollow($user){
        if (! $user){
            return 0;
        }

        return !! $this->fans()->where('uid',$user->id)->where('status',0)->first();

    }
}
